package com.xtianzhuang.www.controller.user.budget;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VUserBudget;
import com.xtianzhuang.www.entity.VUserBudgetExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteStoryBudgetRequest;
import com.xtianzhuang.www.entity.request.SaveBudgetRequest;
import com.xtianzhuang.www.entity.request.StoryBudgetListRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.DeleteStoryBudgetResponse;
import com.xtianzhuang.www.entity.response.SaveBudgetResponse;
import com.xtianzhuang.www.entity.response.StoryBudgetListResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/user")
public class BudgetController extends BaseController {
	@RequestMapping(value = "/budgetList")
	public ModelAndView budgetList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/budget-list");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping("/ajaxQueryStoryBudgetList")
	@ResponseBody
	@Log(type = LogType.USER)
	public StoryBudgetListResponse queryStoryBudgetList(StoryBudgetListRequest request) {
		StoryBudgetListResponse response = new StoryBudgetListResponse();
		VUserBudgetExample example = new VUserBudgetExample();
		if (request.getStoryId() != null) {
			example.or().andStoryIdEqualTo(request.getStoryId());
			long count = userServiceBiz.storyBudgetListCountQuery(example);
			example.setOffset(request.getOffset());
			example.setLimit(request.getLimit());
			List<VUserBudget> data = userServiceBiz.storyBudgetListQuery(example);
			data.add(new VUserBudget());
			response.setCode(CodeConstants.SUCCESS);
			response.setData(data);
			response.setTotal(count);
		} else {
			response.setCode(CodeConstants.SUCCESS);
			response.setData(new ArrayList<VUserBudget>());
			response.setTotal(0);
		}
		return response;

	}

	@RequestMapping("/ajaxSaveStoryBudget")
	@ResponseBody
	@Log(type = LogType.USER)
	@LogOperate
	public SaveBudgetResponse saveStoryBudget(SaveBudgetRequest request) {
		SaveBudgetResponse response = new SaveBudgetResponse();
		try {
			if (request.getBudgetId() == null) {
				userServiceBiz.addStoryBudget(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("添加成功");
			} else {
				userServiceBiz.updateStoryBudget(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("修改成功");
			}

		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

	@RequestMapping("/ajaxDeleteStoryBudget")
	@ResponseBody
	@Log(type = LogType.USER)
	@LogOperate
	public DeleteStoryBudgetResponse deleteStoryBudget(DeleteStoryBudgetRequest request) {
		DeleteStoryBudgetResponse response = new DeleteStoryBudgetResponse();
		try {
			userServiceBiz.deleteStoryBudget(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}
}
